package com.linkoog.devtools.http.request.http_response.response_status;


import com.intellij.openapi.Disposable;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBLabel;

import javax.swing.*;
import java.awt.*;

public class ResponseStatusPanel extends JPanel implements Disposable {

    private final JBLabel statusLabel;
    private final JBLabel timeLabel;
    private final JBLabel sizeLabel;

    private final static Color empty_color = JBColor.LIGHT_GRAY;
    private final static Color ok_color = Color.decode("#3a7b24");

    private final static Color error_color = JBColor.RED;


    public ResponseStatusPanel() {
        super(new FlowLayout(FlowLayout.LEADING, 12,8));
        setPreferredSize(new Dimension(-1, 30));


        statusLabel = new JBLabel();
        LabeledComponent<JBLabel> statusLabelComponent = LabeledComponent.create(statusLabel, "Status:", BorderLayout.WEST);
        //statusLabelComponent.setPreferredSize(new Dimension(100,30));
        add(statusLabelComponent);



        timeLabel = new JBLabel();
        LabeledComponent<JBLabel> timeLabelComponent = LabeledComponent.create(timeLabel, "Time:", BorderLayout.WEST);
        add(timeLabelComponent);


        sizeLabel = new JBLabel();
        LabeledComponent<JBLabel> sizeLabelComponent = LabeledComponent.create(sizeLabel, "Size:", BorderLayout.WEST);
        add(sizeLabelComponent);

        reset(null, null, null);
    }

    public void reset(String status, String time, String size){
        if (status == null){
            statusLabel.setText("--");
            statusLabel.setForeground(empty_color);

            timeLabel.setText("-- ms");
            timeLabel.setForeground(empty_color);

            sizeLabel.setText("-- bytes");
            sizeLabel.setForeground(empty_color);
            return;
        }

        if (time == null){
            time = "-";
        }

        if (size == null){
            size = "-";
        }


        if ("200".equals(status)){
            statusLabel.setText(status + " ok");
            statusLabel.setForeground(ok_color);

            timeLabel.setText(time + " ms");
            timeLabel.setForeground(ok_color);

            sizeLabel.setText(size + " bytes");
            sizeLabel.setForeground(ok_color);
        } else {
            statusLabel.setText(status + " fail");
            statusLabel.setForeground(error_color);

            timeLabel.setText(time + " ms");
            timeLabel.setForeground(error_color);

            sizeLabel.setText(size + " bytes");
            sizeLabel.setForeground(error_color);
        }

    }

    @Override
    public void dispose() {

    }
}
